package com.demo.bookstore.servlet;

import com.demo.bookstore.bean.User;
import com.demo.bookstore.service.UserService;
import com.demo.bookstore.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author ddone
 * @date 2024/3/12-10:57
 */
@WebServlet("/loginServlet")
public class LoginServlet extends HttpServlet {
    private final UserService userService = new UserServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        User user = new User();
        user.setUsername(req.getParameter("username"));
        user.setPwd(req.getParameter("password"));
        User findUser = userService.findUserByUp(user);
        if(findUser!=null){
            resp.sendRedirect(req.getContextPath()+"/index.html");
        } else {
            req.getRequestDispatcher("/pages/user/login.html").forward(req,resp);
        }


    }
}
